//
//  UIViewControllerEx.swift
//  LED OTA
//
//  Created by rui on 2024/4/12.
//

import UIKit

private let FixedSpaceTag = 909

//extension UIViewController: DJRCompatible {}

public extension DJR where T: UIViewController {
    // MARK: 创建导航栏按钮基础接口
    func setLeftBarButton(_ item: UIBarButtonItem?) {
        if item == nil {
            setLeftBarButtons(nil)
        } else {
            setLeftBarButtons([item!])
        }
    }

    func setLeftBarButtons(_ items: [UIBarButtonItem]?) {
        if items != nil {
            if items!.first?.tag != FixedSpaceTag {
                var newItems: [UIBarButtonItem] = Array.init(items!)
                let spaceItem: UIBarButtonItem? = self.createSpaceItem()
                if spaceItem != nil {
                    newItems.insert(spaceItem!, at: 0)
                }
                self.base.navigationItem.leftBarButtonItems = newItems
            } else {
                self.base.navigationItem.leftBarButtonItems = items
            }
        } else {
//            self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: nil, style: .done, target: nil, action: nil)
            self.base.navigationItem.leftBarButtonItems = nil
        }
    }

    func setRightBarButton(_ item: UIBarButtonItem?) {
        if item == nil {
            setRightBarButtons(nil)
        } else {
            setRightBarButtons([item!])
        }
    }

    func setRightBarButtons(_ items: [UIBarButtonItem]?) {
        guard let list = items else {
            self.base.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: UIView())
            self.base.navigationItem.rightBarButtonItems = [UIBarButtonItem]()
            return
        }
        if list.first?.tag != FixedSpaceTag {
            var newItems: [UIBarButtonItem] = Array.init(list)
            let spaceItem: UIBarButtonItem? = self.createSpaceItem()
            if spaceItem != nil {
                newItems.insert(spaceItem!, at: 0)
            }
            self.base.navigationItem.rightBarButtonItems = newItems
        } else {
            self.base.navigationItem.rightBarButtonItems = list
        }
    }

    // MARK: 设置返回按钮
    func setBackBarButton(title: String, target: Any?, action: Selector?) {
        let item = UIBarButtonItem.newBarButton(title: title, target: target, action: action)
        self.setLeftBarButton(item);
    }
    
    // MARK: 文字
    func setLeftBarButton(title: String, target: Any?, action: Selector?) {
        self.setBaseBarButton(title: title, target: target, action: action, isLeft: true)
    }
    func setRightBarButton(title: String, target: Any?, action: Selector?) {
        self.setBaseBarButton(title: title, target: target, action: action, isLeft: false)
    }
    
    private func setBaseBarButton(title: String, target: Any?, action: Selector?, isLeft: Bool) {
        let item = UIBarButtonItem.newBarButton(title: title, target: target, action: action)
        if isLeft {
            self.setLeftBarButton(item)
        } else {
            self.setRightBarButton(item)
        }
    }
    
    // MARK: 图片
    func setLeftBarButton(image: UIImage, target: Any?, action: Selector?) {
        self.setBaseBarButton(image: image, target: target, action: action, isLeft: true)
    }
    func setRightBarButton(image: UIImage, target: Any?, action: Selector?) {
        self.setBaseBarButton(image: image, target: target, action: action, isLeft: false)
    }
    
    private func setBaseBarButton(image: UIImage, target: Any?, action: Selector?, isLeft: Bool) {
        let item = UIBarButtonItem.newImageBarButtom(image, target: target, action: action)
        if isLeft {
            self.setLeftBarButton(item)
        } else {
            self.setRightBarButton(item)
        }
    }

    //  获取导航栏 左 第一个按钮
    func getLeftBarButtomItem() -> UIBarButtonItem? {
        return _getBaseBarButton(index: 0, isLeft: true)
    }
    //  获取导航栏 左 第index个按钮
    func getLeftBarButtomItem(index: NSInteger) -> UIBarButtonItem? {
        return _getBaseBarButton(index: index, isLeft: true)
    }
    //  获取导航栏 右 第一个按钮
    func getRightBarButtomItem() -> UIBarButtonItem? {
        return _getBaseBarButton(index: 0, isLeft: false)
    }
    //  获取导航栏 右 第index个按钮
    func getRightBarButtomItem(index: NSInteger) -> UIBarButtonItem? {
        return _getBaseBarButton(index: index, isLeft: false)
    }


    private func _getBaseBarButton(index: NSInteger, isLeft: Bool) -> UIBarButtonItem? {
        var items: [UIBarButtonItem]? = self.base.navigationItem.rightBarButtonItems
        if isLeft {
            items = self.base.navigationItem.leftBarButtonItems
        }
        items = items?.filter({ (barBtnItem) -> Bool in
            if barBtnItem.tag == FixedSpaceTag {
                return false
            }
            return true
        })
        if items != nil {
            for (offset, result) in items!.enumerated() {
                if index == offset {
                    return result
                }
            }
        }
        return nil
    }
    
    // 获取导航栏 左 按钮
    func getLeftBarButtomItems() -> [UIBarButtonItem]? {
        return _getBaseBarButtons(isLeft: true)
    }
    // 获取导航栏 右 按钮
    func getRightBarButtomItems() -> [UIBarButtonItem]? {
        return _getBaseBarButtons(isLeft: false)
    }
    
    private func _getBaseBarButtons(isLeft: Bool) -> [UIBarButtonItem]? {
        var items: [UIBarButtonItem]? = self.base.navigationItem.rightBarButtonItems
        if isLeft {
            items = self.base.navigationItem.leftBarButtonItems
        }
        return items?.filter({ (barBtnItem) -> Bool in
            if barBtnItem.tag == FixedSpaceTag {
                return false
            }
            return true
        })
    }
    
    // 设置 左按钮 文字
    func setLeftBarButton(title: String?) {
        setLeftBarButton(title: title, index: 0)
    }
    func setLeftBarButton(title: String?, index: NSInteger) {
        let item = _getBaseBarButton(index: index, isLeft: true)
        item?.title = title
    }
    // 设置 右按钮 文字
    func setRightBarButton(title: String?) {
        setRightBarButton(title: title, index: 0)
    }
    func setRightBarButton(title: String?, index: NSInteger) {
        let item = _getBaseBarButton(index: index, isLeft: false)
        item?.title = title
    }

    private func createSpaceItem() -> UIBarButtonItem? {
        var isCan = false
        if #available(iOS 10.0, *) {
            isCan = true
        }
        if isCan {
            let spaceItem: UIBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
            spaceItem.tag = FixedSpaceTag
            return spaceItem
        }
        return nil
    }

}

public extension UIBarButtonItem {
    
    // MARK: UIBarButtonItem 分类 创建BarButtonItem
    
    class func newNormalBarButtom(title: String, target: Any?, action: Selector?) -> UIBarButtonItem {
        let item = UIBarButtonItem.init(title: title, style: .plain, target: target, action: action)
        var attributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16.0)]
        if #available(iOS 11.0, *) {
            let style: NSMutableParagraphStyle = NSMutableParagraphStyle.init()
            style.headIndent = 0.0
            style.lineSpacing = 2.0
            attributes.updateValue(style, forKey: NSAttributedString.Key.paragraphStyle)
        }
        item.setTitleTextAttributes(attributes, for: .normal)
        item.setTitleTextAttributes(attributes, for: .highlighted)
        return item
    }
    
    class func backBarButton(title: String?, target: Any?, action: Selector?) -> UIBarButtonItem {
        let image = UIImage(named: "barbutton_back_black", in: Bundle.module, compatibleWith: nil)
        return newBarButton(title: title, image: image, target: target, action: action)
    }
    
    class func newBarButton(title: String?, image: UIImage? = nil, target: Any?, action: Selector?) -> UIBarButtonItem {
        var t: String = " "
        if title != nil {
            t = title!
        }
        let item: UIBarButtonItem = UIBarButtonItem.init(title: t, style: .plain, target: target, action: action)
        item.tintColor = UIColor.gray
        item.setBackgroundImage(image?.resizableImage(withCapInsets: UIEdgeInsets.init(top: 0.0, left: (image?.size.width) ?? 10.0, bottom: 0.0, right: 0.0)), for: .normal, barMetrics: UIBarMetrics.default)
        var attributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16.0)]
        if #available(iOS 11.0, *) {
            let style: NSMutableParagraphStyle = NSMutableParagraphStyle.init()
            style.headIndent = 0.0
            style.lineSpacing = 2.0
            style.firstLineHeadIndent = (image?.size.width) ?? 10.0
            attributes.updateValue(style, forKey: NSAttributedString.Key.paragraphStyle)
        } else {
            item.setTitlePositionAdjustment(UIOffset.init(horizontal: 5, vertical: 0), for: UIBarMetrics.default)
        }
        item.setTitleTextAttributes(attributes, for: .normal)
        item.setTitleTextAttributes(attributes, for: .highlighted)
        return item
    }
    
    class func newImageBarButtom(_ image: UIImage?, target: Any?, action: Selector?) -> UIBarButtonItem {
        let btn: UIButton = UIButton.init(type: .custom)
        btn.setImage(image, for: .normal)
        btn.sizeToFit()
        if action != nil {
            btn.addTarget(target, action: action!, for: .touchUpInside)
        }
        let item: UIBarButtonItem = UIBarButtonItem.init(customView: btn)
            return item
    }
}
